//
//  COCallSessionController.swift
//  CocoProject
//
//  Created by Vill on 2023/11/15.
//

import UIKit

struct Log {
    enum ContentType {
        case mine, other
        
        // 内容字颜色
        var txtColor:UIColor{
            switch self {
            case .other :  return UIColor.colorWithHex("#FFFFFF")
            case .mine  :  return UIColor.colorWithHex("#FFFFFF")
            }
        }
    }

    var type       : ContentType
    var content    : String           // 内容
    var nickName   : String           // 昵称
}

class COCallSessionController: UIViewController,UITableViewDelegate,UITableViewDataSource {

    private lazy var list = [Log]()
    
    lazy var baseTableView: UITableView = {
        let tableView = UITableView(frame: .zero, style: .plain)
        tableView.dataSource = self
        tableView.delegate = self
        tableView.backgroundColor = .clear
        tableView.tableFooterView = UIView()
        tableView.showsVerticalScrollIndicator = false
        if #available(iOS 11.0, *) {
            if #available(iOS 13.0, *) {
                tableView.automaticallyAdjustsScrollIndicatorInsets = false
            }else{
                tableView.contentInsetAdjustmentBehavior = .never
            }
        }else {
            automaticallyAdjustsScrollViewInsets = false
        }
        return tableView
    }()
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor.clear
        view.addSubview(baseTableView)
        baseTableView.backgroundColor = UIColor.clear
        baseTableView.separatorStyle = .none
        baseTableView.rowHeight = UITableView.automaticDimension
        baseTableView.estimatedRowHeight  = PTW(50)
        baseTableView.snp.makeConstraints { make in
            make.leading.trailing.equalToSuperview()
            make.top.equalToSuperview().offset(PTW(10))
            make.bottom.equalToSuperview().offset(-PTW(10))
        }
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return list.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell:VideoChatTableCell = VideoChatTableCell().cellWithTableView(tableView)
        cell.selectionStyle = .none
        cell.update(list[indexPath.row])
        return cell
    }
    
    func log(type: Log.ContentType, content: String, nickName:String) {
        let log = Log(type: type, content: content,nickName:nickName)
        list.append(log)
        let index = IndexPath(row: list.count - 1, section: 0)
        baseTableView.insertRows(at: [index], with: .bottom)
        baseTableView.scrollToRow(at: index, at: .bottom, animated: false)
    }
}
